<?php
/** .-------------------------------------------------------------------
 * |  Software: [HDCMS framework]
 * |      Site: www.hdcms.com
 * |-------------------------------------------------------------------
 * |    Author: 向军 <2300071698@qq.com>
 * |    WeChat: aihoudun
 * | Copyright (c) 2012-2019, www.houdunwang.com. All Rights Reserved.
 * '-------------------------------------------------------------------*/

namespace web\admin\controller;
/**
 * Class Article
 * 文章列表
 */

class Article extends Common{
    //动作
	protected $db;
	public function __construct()
	{
		parent::__construct();
		$this->db = new \system\model\Article();
	}
//	首页
	public function index(){
		$data = $this->db->getAll(1);
		View::with('data',$data)->make();
	}
//	添加
	public function add(){
		if (IS_POST){
			if ($this->db->stroe()){
				//				echo json_encode(['valid'=>1,'message'=>'操作成功']);
				//message这里如果是异步请求数据等价于上边json_encode
				message('操作成功','','success');
			}
			message($this->db->getError(),'','error');
		}
		//获取所属分类数据
		$cateData =Data::tree(Db::table("category")->get(),'cname');
		//获取标签数据
		$tagData = Db::table("tag")->get();
		View::with('cateData',$cateData)->with('tagData',$tagData)->make();
	}
	public function edit(){
		$aid = q('get.aid',0,'intval');
		if (IS_POST){
			if ($this->db->edit()){
				echo json_encode(['valid'=>1,'message'=>'编辑成功']);exit;
			}
			message($this->db->getError(),'','error');
		}
        //1.获取旧数据
		$oldData = $this->db->join('article_data','aid','=','article_aid')->where('aid',$aid)->first();
		View::with('oldData',$oldData);
        //获取所有分类数据
		$cateData = Db::table('category')->get();
		$cateData = Data::tree($cateData,'cname');
		View::with('cateData',$cateData);
        //获取所有标签数据
		$tagData = Db::table('tag')->get();
		View::with('tagData',$tagData);
        //获取文章添加的时候，添加的标签
		$arcTag = Db::table('article_tag')->where('article_aid',$aid)->lists('tag_tid');
		View::with('arcTag',$arcTag);
		View::make();
	}
    //回收站首页
    public function recycle(){
        $data=$this->db->getAll(2);
        View::with('data',$data);
        View::make();
    }
    //删除到回收站
    public function delRecycle(){
        $aid = q('get.aid',0,'intval');
        //将is_recycle字段修改为2
        $this->db->where('aid',$aid)->update(['is_recycle'=>2]);
        message('删除成功到回收站',u('index'),'sueecss');
    }
    //回收站还原
    public function reduction(){
        $aid = q('get.aid',0,'intval');
        //将is_recycle字段修改为1
        $this->db->where('aid',$aid)->update(['is_recycle'=>1]);
        message('还原成功',u('index'),'sueecss');
    }
    //回收站真正的删除
    public function realDel(){
        if(IS_POST)
        {
            if($this->db->del()){
                message("删除成功",'','success');
            }
            message($this->db->getError(),'','error');
        }
    }
//    异步删除缩略图
    public function delImg(){
        //删除文件
        if(file_exists($_POST['path'])){
            unlink($_POST['path']);
        }
    }
}
